package com.wxg.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

/**
 * 工具类
 * @author Wxg
 *
 */
public class My {
	
	public static Properties staticProp;
	public static final String SP = File.separator;
	

	private static int ssid = 1;
	private static String ssOld = "";
	private static SimpleDateFormat ssSdf = new SimpleDateFormat("yyyyMMdd_HHmm");
	
	/**
	 * 返回classes文件根目录地址
	 * @return
	 */
	public static String rootpath(){
		String rootpath = My.class.getClassLoader().getResource("").getPath();
		return rootpath;
	}
	
	/**
	 * 拿到资源文件的地址
	 * @param relativePath
	 * @return
	 */
	public static String rootpath(String relativePath){
		String filepath = My.class.getClassLoader().getResource(relativePath).getPath();
		return filepath;
	}
	
	/**
	 * 读取资源文件,按行返回,以数组形式!
	 * @param resPath
	 * @return
	 */
	public static String[] lines(String resPath){
		String filepath = rootpath(resPath);
		BufferedReader br = null;
		List<String> tempList = new ArrayList<String>();
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filepath)), "UTF8"));
			String line = null;
			while( (line=br.readLine()) != null ){
				line = line.trim();
				if("".equals(line)){
					continue;
				}
				if(line.startsWith("#")){
					continue;
				}
				tempList.add(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != br){
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return tempList.toArray(new String[]{});
	}
	
	/**
	 * 获取"properties"文件
	 * @param resPath
	 * @return
	 */
	public static Properties prop(String resPath){
		InputStream is = My.class.getClassLoader().getResourceAsStream(resPath);
		Properties prop = new Properties();
		try {
			prop.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(null != is){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return prop;
	}
	
	/**
	 * 1) https://mvnrepository.com/artifact/com.google.guava/guava
	 * -	* guava
	 * 2) https://mvnrepository.com/artifact/org.jodd/jodd-core
	 * -	* jodd
	 * 3) https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
	 * -	* commons
	 * 
	 * 
	 * @see java.util.Date
	 * @see  java.text.SimpleDateFormat
	 * 
	 * 使用 My.ss()
	 * @return
	 */
	@Deprecated
	public static String ss(int id){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmm");
		String idstr = StringUtils.leftPad(String.valueOf(id), 3, "0");
		String dateStr = sdf.format(new java.util.Date());
		return dateStr + idstr;
	}
	
	public static String ss(){
		return ss(null);
	}
	
	/**
	 * 产生一个字符串(prefix + 年月日_时分)
	 * 
	 * @param prefix
	 * @return
	 */
	public static String ss(String prefix){
		if(null == prefix) prefix = "";
		String dateStr = ssSdf.format(new java.util.Date());
		if(!ssOld.equals(dateStr)){
			ssOld = dateStr;
			ssid = 1;
		}
		String idstr = StringUtils.leftPad(String.valueOf(ssid++), 3, "0");
		return prefix + dateStr + idstr;
	}
	
	/**
	 * 获取文件的url
	 * 
	 * @param filepath
	 * @return
	 */
	public static String fileUrl(String filepath){
		if(null == filepath) return null;
		File file = new File(filepath);
		URL url = null;
		
		try {
			url = file.toURI().toURL();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		
		if(null != url){
			return url.toString();
		}
		return null;
	}
	
	/**
	 * 类似JFinal的Prop.use()
	 * 
	 * @param key
	 * @return
	 */
	public static String conf(String key){
		if(null == staticProp){
			staticProp = prop("mainconf.properties");
		}
		if(null != staticProp){
			return staticProp.getProperty(key);
		}
		return null;
	}
	
	/**
	 * Add 2018-2-6
	 * str在数组中
	 * 
	 * @param target
	 * @param arr
	 * @return
	 */
	public static boolean in(String target, String[] arr){
		if(null == target || null == arr) return false;
		for(String str : arr){
			if(str.equals(target)){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Add 2018-2-6
	 * str不在数组中
	 * 
	 * @param target
	 * @param arr
	 * @return
	 */
	public static boolean notIn(String target, String[] arr){
		if(null == target || null == arr) return false;
		return !in(target, arr);
	}
	
	
	public static void main(String[] args) {
		for(int i=0;i<100;i++){
			System.out.println( ss() );
		}
	}
	
	
	
	
}
